package com.java.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.common.model.Result;
import com.java.manage.dao.ModelMapper;
import com.java.manage.model.Color;
import com.java.manage.model.Model;
import com.java.manage.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ModelServiceImpl implements ModelService {
    @Autowired
    private ModelMapper modelMapper;

    @Override
    public Result save(Model model) {
        modelMapper.save(model);
        return Result.success();
    }
    @Override
    public Result modify(Model model) {
        modelMapper.modify(model);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            modelMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result list(Model model) {
        PageHelper.startPage(model.getPageNum(), model.getPageSize());
        List<Model> list = modelMapper.list(model);
        PageInfo<Model> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result get(int id) {
        return null;
    }

    @Override
    public Result modelList() {
        List<Model> models = modelMapper.modelList();
        return Result.success(models);
    }
}
